import { _decorator, Color, Component, instantiate, Node, Prefab, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UIHeader } from '../common/UIHeader';
import { TabLayout } from '../common/TabLayout';
import { EventManager } from '../event/EventManager';
import { UserInfo } from '../common/UserInfo';
import { Ally } from '../entity/Ally';
import ResManager from '../common/ResManager';
const { ccclass, property } = _decorator;

@ccclass('UnionIndex')
export class UnionIndex extends Component {
    @property(UIHeader)
    uiHeader:UIHeader
    @property(TabLayout)
    tabLayout:TabLayout
    
    @property(Node)
    content:Node

    async start() {
        this.uiHeader.setData('联盟')
        EventManager.instance.on('联盟close', this.onClose, this)

        EventManager.instance.on(EventManager.EventType.AllyRedDot, this.onAllyRedDot, this)

        await UserInfo.instance.refreshVillageDetail()
        let myAlly = Ally.getMyAlly()
        if (myAlly) {
            if(myAlly.isLeader){
                this.tabLayout.setData(['联盟概况','联盟成员','入盟申请','我的邀请'])
            } else {
                this.tabLayout.setData(['联盟概况','联盟成员'])
            }
           
            EventManager.instance.on('联盟概况', this.onInfo, this )
            EventManager.instance.on('联盟成员', this.onMember, this )
            EventManager.instance.on('入盟申请', this.onOtherApply, this )
            EventManager.instance.on('我的邀请', this.onInviteOther, this )

            this.onInfo()
        } else {
            this.tabLayout.setData(['联盟列表','创建联盟','入盟邀请', '我的申请'])
            EventManager.instance.on('联盟列表', this.onList, this )
            EventManager.instance.on('创建联盟', this.onCreate, this )
            EventManager.instance.on('入盟邀请', this.onInviteMe, this )
            EventManager.instance.on('我的申请', this.onMeApply, this )
            
            this.onList()
        }


    }

    async onList(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.AllyList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }
    async onCreate(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.CreateAlly)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }
    async onInviteMe(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.InviteMeList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }

    async onMeApply(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.MeApplyList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }
    async onInfo(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.AllyInfo)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }
    async onMember(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.MemberList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }
    onNews(){
        
    }
    async onInviteOther(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.InviteOtherList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }

    async onOtherApply(){
        this.content.destroyAllChildren()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.OtherApplyList)
        let node = instantiate(prefab)
        this.content.addChild(node)
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.AllyIndex)
    }

    onAllyRedDot(data){
        let InviteList = data.InviteList
        let InvitedList = data.InvitedList
        let ReqList = data.ReqList
        let ReqedList = data.ReqedList

        if(ReqedList && ReqedList.length > 0) {
            this.tabLayout.setRedDot('入盟申请', true)
        } else {
            this.tabLayout.setRedDot('入盟申请', false)
        }

        if(InviteList && InviteList.length > 0){
            this.tabLayout.setRedDot('入盟邀请', true)
        } else {
            this.tabLayout.setRedDot('入盟邀请', false)
        }
    }
   
}


